﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;

namespace GuildStock.Services.JsonToDynamic
{
  /// <summary>
  /// Json helper class
  /// </summary>
  public class JsonHelper
  {
    /// <summary>
    /// Converts json string to dynamic object.
    /// </summary>
    public static dynamic ConvertJsonToDynamic(string json)
    {
      try
      {
        dynamic entity = null;
        if (json != null)
        {
          JavaScriptSerializer jss = new JavaScriptSerializer();
          jss.RegisterConverters(new JavaScriptConverter[] { new DynamicJsonConverter() });

          entity = jss.Deserialize(json, typeof(object)) as dynamic;
        }
        return entity;
      }
      catch
      {
        return null;
      }
    }
  }
}